// pages/selection/selection.js
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    movieId: '',
    cinemaId: '',
    cinemaName: '',
    movieName: '',
    price: NaN,
    roomName: '',
    seat: [
      [1, 1, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    ],
    date: '',
    timeRange: '',
    selectedCnt: 0,
    selectedSeat: [],
    hasBuySeat: [],
    seatImg: [],
    grid_w: 'cloud://zjn-0gzb74lr2a7692b1.7a6a-zjn-0gzb74lr2a7692b1-1317297115/image/grid_w.png',
    grid_g: 'cloud://zjn-0gzb74lr2a7692b1.7a6a-zjn-0gzb74lr2a7692b1-1317297115/image/grid_g.png',
    grid_r: 'cloud://zjn-0gzb74lr2a7692b1.7a6a-zjn-0gzb74lr2a7692b1-1317297115/image/grid_r.png',
    delete_icon: 'cloud://zjn-0gzb74lr2a7692b1.7a6a-zjn-0gzb74lr2a7692b1-1317297115/image/delete.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    // 获取影院的场次信息
    let pages = getCurrentPages(); 
    let currentPage = pages[pages.length-1];
    var options = currentPage.options
    var that = this
    var sessionId = options.sessionId
    console.log(sessionId)
    await db.collection('session_info').where({
      _id: sessionId
    }).get().then(res => {
      var hasBuySeat = []
      var seatImg = []
      var seat = res.data[0].seat
      for (let i in seat) {
        var rowImg = []
        for (let j in seat[i]) {
          if (seat[i][j] === 0) {
            hasBuySeat.push([parseInt(i), parseInt(j)])
            rowImg.push(that.data.grid_r)
          } else {
            rowImg.push(that.data.grid_w)
          }
        }

        seatImg.push(rowImg)
      }

      that.setData({
        id: res.data[0]._id,
        movieId: res.data[0].movieId,
        cinemaId: res.data[0].cinemaId,
        date: res.data[0].date,
        timeRange: res.data[0].startTime + '-' + res.data[0].endTime,
        roomName: res.data[0].room,
        price: res.data[0].price,
        seat: seat,
        selectedCnt: 0,
        selectedSeat: [],
        hasBuySeat: hasBuySeat,
        seatImg: seatImg
      })
    })

    await db.collection('cinema').where({
      _id: that.data.cinemaId
    }).get().then(res => {
      that.setData({
        cinemaName: res.data[0].funTitle,
      })
    })

    await db.collection('movies').where({
      _id: that.data.movieId
    }).get().then(res => {
      that.setData({
        movieName: res.data[0].title
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  selectSeat(e) {
    var i = e.target.dataset.i
    var j = e.target.dataset.j
    for (let idx in this.data.hasBuySeat) {
      if (this.data.hasBuySeat[idx][0] === i && this.data.hasBuySeat[idx][1] === j) {
        return
      }
    }

    if (this.data.selectedCnt === 6 && this.data.seat[i][j] === 1) {
      wx.showToast({
        title: '一次最多购买6张票',
        icon: 'none',
        duration: 1500
      })

      return
    }

    var seatItem = [i, j]
    var seat = this.data.seat
    var selectedCnt = this.data.selectedCnt
    var selectedSeat = this.data.selectedSeat
    var seatImg = this.data.seatImg

    seat[i][j] = 1 - seat[i][j]
    this.setData({seat: seat})

    if (seat[i][j] === 0) {
      selectedCnt++
      selectedSeat.push(seatItem)
      seatImg[i][j] = this.data.grid_g
    } else {
      selectedCnt--
      selectedSeat = selectedSeat.filter(item => {
        return item[0] !== seatItem[0] || item[1] !== seatItem[1]
      })
      seatImg[i][j] = this.data.grid_w
    }

    this.setData({
      selectedCnt: selectedCnt,
      selectedSeat: selectedSeat,
      seatImg: seatImg
    })
  },

  deleteSelectedSeat(e) {
    var i = e.target.dataset.i
    var selectedSeat = this.data.selectedSeat
    var seat = this.data.seat
    var x = selectedSeat[i][0]
    var y = selectedSeat[i][1]
    var selectedCnt = this.data.selectedCnt

    selectedCnt--
    seat[x][y] = 1
    selectedSeat.splice(i, 1)

    this.setData({
      selectedSeat: selectedSeat,
      selectedCnt: selectedCnt,
      seat: seat
    })
  },

  onSubmit() {
    var that = this
    wx.navigateTo({
      url: `../selection_res/selection_res?id=${that.data.id}&selectedSeat=${JSON.stringify(that.data.selectedSeat)}`,
    })
  }
})